package pl.enigmatic.math.interpolation;

public class BezierSplineInterpolator extends Interpolator.Single {

	public static double h00(final double t) {
		return (1 + 2 * t) * (1 - t) * (1 - t);
	}

	public static double h10(final double t) {
		return t * (1 - t) * (1 - t);
	}

	public static double h01(final double t) {
		return t * t * (3 - 2 * t);
	}

	public static double h11(final double t) {
		return t * t * (t - 1);
	}

	double left[];
	double right[];

	public BezierSplineInterpolator(final double[] frames, final double[] values, final double[] left, final double[] right) {
		super(frames, values);
		this.left = left;
		this.right = right;
	}

	protected double getValue(final int rng, final double alpha) {
		double p0 = value(rng);
		double p1 = value(rng + 1);
		
		final double dt = frame(rng + 1) - frame(rng);
		double v0 = this.left[rng] * dt;
		double v1 = this.right[rng + 1] * dt;
		
		return h00(alpha) * p0 + h10(alpha) * v0 + h01(alpha) * p1 + h11(alpha) * v1;
	}

	@Override
	protected double getValue(final double alpha, final int rng, final double t) {
		return getValue(rng, t);
	}
}
